package net.sourceforge.ftgl.math;



/**
 * Some methods, to make the life easier.
 * @author funsheep
 */
public class FTMath
{

	public static final float length(float [] vec)
	{
		return (float) Math.sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
	}
	
	public static final float dot(float [] vec1, float [] vec2)
	{
		return vec1[0] * vec2[0] + vec1[1] * vec2[1] + vec1[2] * vec2[2];
	}
	
	public static final float angle(float [] normal1, float [] normal2)
	{
		float dls = dot(normal1, normal2) / (length(normal1) * length(normal2));
		if (dls < -1.0f)
			dls = -1.0f;
		else if (dls > 1.0f)
			dls = 1.0f;
		return (float)Math.acos(dls);
	
	}
	
	public static final float [] add(float [] v1, float [] v2)
	{
		return new float[]{ v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2] };
	}

	public static final float [] scaleGiven(float [] v, float s)
	{
		v[0] *= s;
		v[1] *= s;
		v[2] *= s;
		return v;
	}
	
	public static final float [] scaleGiven(float [] v, float x, float y, float z)
	{
		v[0] *= x;
		v[1] *= y;
		v[2] *= z;
		return v;
	}
	
	/**
	 * Generates the next power of two.
	 * @param in An int.
	 * @return The next power of two.
	 */
	public static final int nextPowerOf2(int in)
	{
		 in -= 1;

		 in |= in >> 16;
		 in |= in >> 8;
		 in |= in >> 4;
		 in |= in >> 2;
		 in |= in >> 1;

		 return in + 1;
	}

	public static int hashLong(long l)
	{
		return (int)(l ^ (l >>> 32));
	}

}